package com.ruoyi.rsbi.component;

import com.rsbi.ext.engine.init.TemplateManager;
import com.rsbi.ext.engine.init.VelocityManager;
import com.rsbi.ext.engine.init.XmlParser;
import com.rsbi.ext.engine.util.RuleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.lang.reflect.Field;

@Slf4j
public class ExtContextLoaderListener implements ServletContextListener {

    public ExtContextLoaderListener() {
    }

    public void contextInitialized(ServletContextEvent event) {
        try {
            ClassPathResource classPathResource = new ClassPathResource("ext2/ext-config/ext-config.xml");
            File file = classPathResource.getFile();
            String path = file.getAbsolutePath()
                    //格式化Windows路径
                    .replace("\\", "/")
                    //清除多余路径
                    .replace("/ext2/ext-config/ext-config.xml", "")
                    + "/";
            Field absolutePath = RuleUtils.class.getDeclaredField("realPath");
            absolutePath.setAccessible(true);
            absolutePath.set(null, path);
            TemplateManager.initTemplate(event.getServletContext());
            VelocityManager.initVelocity(event.getServletContext());
            XmlParser.initParser(event.getServletContext());
            XmlParser.getInstance().parserConfig();
        } catch (Exception var5) {
            log.error("初始化配置信息出错.", var5);
        }
    }

    public void contextDestroyed(ServletContextEvent event) {

    }
}
